// pages/submitDetail/index.js
const app = getApp()
const req=require('../../utils/req')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    value:'',
    isPopup:false,
    radio:'1',
    isOpen:false,
    id:'',
    dataObj:{},
    payType:2,
    remake:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.hideShareMenu()
    let that = this
    if(options && options.id && options.type){
        this.setData({
          id:parseInt(options.id),
          type:parseInt(options.type)
        })
        this.getOrderDetail()
    }else{

    }
    /*
    var eventChannel = that.getOpenerEventChannel()
    eventChannel.on('submitDetail', function (data) {
      if(data.type==1){
        that.setData({
          dataObj:data.object,
          id:data.id
        })
      }else{
        that.setData({
          id:data.id
        })
        that.userOrderDetails()
      }
      
      console.log(data)
      
    })*/

  },
  openList(){
    this.setData({
      isOpen:!this.data.isOpen
    })
  },
  onClose(){
    this.setData({
      isPopup:false
    })
  },
  onClick(e){
    this.setData({
      isPopup:false,
      radio:e.currentTarget.dataset.name.toString()
    })
    console.log(this.data.radio)
  },
  openPopup(){
    this.setData({
      isPopup:true
    })
  },
  gotoEnd(){
    wx.showLoading({
      title: '加载中',
      mask:true
    })
    let payData = this.data.dataObj.orderDetail
    let data = {
      orderId:this.data.id,
      userId:wx.getStorageSync('userLogin').userId
    }
    if(this.data.remake){
      data.remake=this.data.remake
    }

    app.agriknow.userPayOrder(data).then(res=>{
      wx.hideLoading()
      console.log(res)
      
      let payData = JSON.parse(res.data.payInfo)
  
      wx.requestPayment({
        timeStamp: payData.timeStamp,
        nonceStr: payData.nonceStr,
        package: payData.package,
        signType: payData.signType,
        paySign: payData.paySign,
        success(res) {
          console.log('支付成功')
          setTimeout(function(){
            wx.reLaunch({
              url: '/pages/myOrder/index',
            })
          },2000)
         
        },
        fail(res) {
          console.log('失败：' + res)
        }
      })
    })
  
    
  },

  userOrderDetails(){
    let data = {
      id:this.data.id
    }
    app.agriknow.userOrderDetails(data).then(res=>{
      console.log(res)
      this.setData({
        dataObj:res.data
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  getOrderDetail:function(e){
    var that=this
    var id=this.data.id
    var data={}
    data.id=id
    var type=this.data.type
    var url=''
    if(type==1){
      url='/ht/user/userQrOrderDetails'
    }else if(type==2){
      url='/ht/user/userOrderDetails'
    }else{
      wx.showToast({
        title: '参数错误',
        icon:'none',
        duration:1500
      })
      return false
    }
    req.request(url,data,'POST').then(res=>{
      that.setData({
        detail:res.data
      })
    })
  },
  //选择支付方式
  selectPay:function(e){
    var type=e.currentTarget.dataset.type
    this.setData({
      payType:type
    })
  },
  pay:function(){
    var data={}
    var locationInfo=wx.getStorageSync('locationInfo')
    if(!locationInfo){
     
    }
   if(locationInfo){
     var location=locationInfo.location
    data.location=location.lat+','+location.lng
   }
   data.orderId=this.data.id
    req.request('/ht/user/yplPayOrder',data,'POST').then(res=>{
     console.log(res)
    })
  },
   //获取用户地理位置权限
 getPermission:function(obj){
  wx.chooseLocation({
  success: function (res) { 
   obj.setData({
    addr: res.address  //调用成功直接设置地址
   })    
  },
  fail:function(){
   wx.getSetting({
    success: function (res) {
     var statu = res.authSetting;
     if (!statu['scope.userLocation']) {
      wx.showModal({
       title: '是否授权当前位置',
       content: '需要获取您的地理位置，请确认授权，否则地图功能将无法使用',
       success: function (tip) {
        if (tip.confirm) {
         wx.openSetting({
          success: function (data) {
           if (data.authSetting["scope.userLocation"] === true) {
            wx.showToast({
             title: '授权成功',
             icon: 'success',
             duration: 1000
            })
            //授权成功之后，再调用chooseLocation选择地方
            wx.chooseLocation({
             success: function(res) {
              obj.setData({
               addr: res.address
              })
             },
            })
           } else {
            wx.showToast({
             title: '授权失败',
             icon: 'success',
             duration: 1000
            })
           }
          }
         })
        }
       }
      })
     }
    },
    fail: function (res) {
     wx.showToast({
      title: '调用授权窗口失败',
      icon: 'success',
      duration: 1000
     })
    }
   })
  }
 })  
 },

 input:function(e){
   var key=e.currentTarget.dataset.key
   this.setData({
     [key]:e.detail.value
   })
 }
})